﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Globalization;

using DTO;
using DataService;

public partial class Admin_AddDevice :BasePage
{
    protected int customerId = 0;
    int deviceId = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (AdminLogin == null)
            Response.Redirect("~/admin/login.aspx");

        Initial();
    }

    private void Initial()
    {
        if ( !string.IsNullOrEmpty(Request.Params["c"]) )
        {
            int.TryParse(Request.Params["c"], out customerId);
        }

        if ( customerId == 0 )
            Response.Redirect("~/admin/default.aspx");

        using (CustomerService service = new CustomerService())
        {
            litCustomer.Text = service.GetAddressInfoByAccountId(customerId).ToString();
        }

        int.TryParse(Request.Params["d"], out deviceId);


        if ( !IsPostBack )
        {
            CalendarFromDate.Format = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
            CalendarToDate.Format = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;

            using ( DeviceService service = new DeviceService() )
            {
                ddlOS.DataSource = service.GetListOS();

                if ( deviceId > 0 )
                {
                    Device device = service.GetDevice(deviceId);
                    txtName.Text = device.Name;
                    txtCpu.Text = device.CPU;
                    txtDiskQuota.Text = device.DiskQuota;
                    txtRam.Text = device.RAM;
                    txtIPAddress.Text = device.IP;
                    ddlOS.Text = device.OS.Id.ToString();
                    txtFee.Text = device.Fee.ToString();
                    txtFromDate.Text = ConvertDateToString(device.RentFromDate);
                    txtToDate.Text = ConvertDateToString(device.RentToDate);
                }
                DataBind();
            }
        }
    }

    protected void btnSave_Click ( object sender, EventArgs e )
    {
        double fee = 0;
        double.TryParse(txtFee.Text, out fee);

        int osId = 0;
        int.TryParse(ddlOS.SelectedValue, out osId);

        DateTime from = ConvertStringToDate(txtFromDate.Text);
        DateTime to = ConvertStringToDate(txtToDate.Text);

        if (fee == 0 || osId == 0 || from == DateTime.MinValue || to == DateTime.MinValue) return;

        Device device = new Device(deviceId)
        {
            Name = txtName.Text,
            CPU = txtCpu.Text,
            DiskQuota = txtDiskQuota.Text,
            RAM = txtRam.Text,
            IP = txtIPAddress.Text,
            Fee = fee,
            Bandwidth = txtBandWidth.Text,
            RentFromDate = from,
            RentToDate = to,
            OS = new Device.OperatingSystem(osId) { Name = ddlOS.Text },
            Customer = new Customer(customerId) {}
        };

        using ( DeviceService service = new DeviceService() )
        {
            //service.SaveDevice(device);
        }

        Response.Redirect("~/admin/devices.aspx?ac=" + customerId);
    }

    protected void btnCancel_Click ( object sender, EventArgs e )
    {
        Response.Redirect("~/admin/firewalls.aspx?ac=" + customerId);
    }
}